package com.sgzn.sgpay.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

public class PropertiesFileUtil {
    private static final String CONFIG_FILE = "/properties/app.properties";
    private static Properties configProps = new Properties();

    public PropertiesFileUtil() {
    }

    public static void init() {
        if (configProps.isEmpty()) {
            loadPropertyFile();
        }
    }

    private static void loadPropertyFile() {
        InputStream fi = null;
        try {
            fi = PropertiesFileUtil.class.getResourceAsStream(CONFIG_FILE);
            configProps.load(fi);
        } catch (IOException ignored) {
        } finally {
            try {
                if (null != fi) {
                    fi.close();
                }
            } catch (IOException e) {
            }
        }
    }

    public static String getProperty(String name) {
        init();
        return configProps.getProperty(name);
    }

    public static String getPropertyUtf(String name) {
        init();
        String pro = configProps.getProperty(name);
        return asc2utf(pro);
    }

    private static String asc2utf(String str) {
        if (str == null) {
            return "";
        }
        try {
            return new String(str.getBytes("ISO-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException ignored) {
        }
        return str;
    }
}
